<?php defined('SYSPATH') or die('No direct script access.');
/**
* User KohanCrud
* Nota de Mejora : Cada "type_FIELD" debe ser una clase, que se debe cargar dependiendo de los metadatos ... asi evita tener type_FIELD 
* cargados Inutilmente y el codigo no se hace tan grande
*/
class fpp_fieldtype_Model extends Model {
  public $sqlfields = NULL;
  private $TypeLoad = array();
  public $field_value = NULL;
  private $PkID = 0;
  
  public function __construct($meta_fields){
    parent::__construct();
    $this->fields = new StdClass();
    $this->_ResHTML = new StdClass();
    $this->_TypesArgs = Kohana::config('fpp_config.type_arguments');
    foreach($meta_fields as $namefield => $typefield){
      $this->_TypeFieldStr = $typefield;
      $this->_NameFieldStr = $namefield;
      $this->types();
    }
    $this->attributes = new StdClass();
  }
  
  public function SetPkID($PkID){
    $this->PkID = (integer) $PkID;
  }
  
  public function AddAttributes($name,$value){
    $this->attributes->$name = $value;
  }
  
  public function types(){
    $typefield = $this->_TypeFieldStr; 
    $this->_Argsobj = null;
    $typefield = explode('(',$typefield,2);
    $type = $typefield[0];
    $this->_Argsobj = substr($typefield[1],0,-1);    
    $this->argument();
    $field = $this->_NameFieldStr;
    $this->fields->$field = $this->_MyArguments;
    $this->fields->$field->typefield = $type;//(string)$ObjType;
    
    return TRUE;
  }

    public function ObjectField($field){    
    $type = $this->fields->$field->typefield ;
    $fname = $this->field_name;
    if(!(isset($this->TypeLoad[$type]))){ 
      $dirf = dirname(__FILE__);
      include_once($dirf.'/types/'.$type.'.php');   
      $this->TypeLoad[$type] = TRUE;
    }

    $DataObject = new StdClass();
    $DataObject->arguments = $this->fields->$fname;
    $DataObject->field_value = $this->field_value;
    $DataObject->field_name = $fname;
    $DataObject->attributes = $this->attributes;
    $DataObject->PkID = $this->PkID;
    $ObjType = new $type($DataObject);
    return (string)$ObjType;
  }

  public function loadpost($field,&$value){
//     if($this->fields->$field->typefield == 'type_DateTimeField'){
//       $value = fpp::datedb($value);
//     }
  }
  
  public function argument(){
    $ArgumentObject = new StdClass();
    $args = $this->_Argsobj;
    $arg_list = explode(',',$args);
    $arguments_list = $this->_TypesArgs;
       foreach ($arg_list as $key => $value) {
         if($value <> ''){
           $arg = explode('=',$value,2);
           $argument = $arg[0];
           $valuearg = @$arg[1];
           $argument = isset($arg[0]) ? $argument : null;
           $valuearg = isset($arg[1]) ? $valuearg : null;
           if(isset($arg[1])){
              if($argument == 'choices'){
                $valuearg = str_ireplace(';',',',$valuearg);
                $valuearg = json_decode($valuearg,TRUE);
              }
              $arguments_list[$argument] = $valuearg;
           }
         }
       }
    $obj = new StdClass();
    foreach($arguments_list as $argument => $value){
      if(is_string($value)){
        $value = strtoupper($value) === 'FALSE' ? FALSE : $value;
        $value = strtoupper($value) === 'TRUE' ? TRUE : $value;
        $value = strtoupper($value) === 'NULL' ? NULL : $value;
      }
      $value = ($argument == 'verbose' and $value == null)  ? $this->_NameFieldStr : $value;
      $obj->$argument = $value;
    }
    $this->_MyArguments = $obj;
    return true;
  }
  


  
 
//   public function type_ImageField(){
//     return form::input($this->field_name,$this->field_value);
//   }
//   
  
  
//   public function type_EmailField(){
//     return form::input($this->field_name,$this->field_value);
//   }
  

//   public function type_IPAddressField(){
//     return form::input($this->field_name,$this->field_value);
//   }
  public function type_TextField(){
    return form::textarea($this->field_name,$this->field_value);
  }
//   public function type_URLField(){
//     return form::input($this->field_name,$this->field_value);
//   }
//   public function type_SmallInteger(){
//     return form::input($this->field_name,$this->field_value,'size="5" maxlength="5"');
//   }


  
}
